package com.hsmpay.qian4.service.ds.impl;

import com.hsmpay.common.pojo.qian4.Qian4SendLog;
import com.hsmpay.common.util.jiami.Des3;
import com.hsmpay.mobile.action.account.fc.PurchaseBaseAction;
import com.hsmpay.mobile.dao.terminal.TchannelMDao;
import com.hsmpay.mobile.pojo.terminal.TchannelM;
import com.hsmpay.qian4.service.ds.ZYTLPayService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;
import com.hsmpay.qian4.pojo.BalanceResult;
import com.hsmpay.qian4.pojo.FeeResult;
import com.hsmpay.qian4.pojo.PurchaseResult;
import com.hsmpay.qian4.pojo.RevocationResult;
import com.hsmpay.utils.FileUtils;
import com.hsmpay.utils.JSONStringBuilder;
import com.hsmpay.utils.WriteFile;
import com.hsmpay.utils.ZytlHttpUrlToPost;
import com.hsmpay.utils.system.ConConfig;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ds_zytlPayService")
public class ZYTLPayServiceImpl implements ZYTLPayService{
	String debug = ConConfig.getAttribute("ZYTL_DAISHOU_DEBUG_STATUS");
	public static WriteFile wf = new WriteFile("ds_zytl");

    @Resource(name="tchannelMDao")
    private TchannelMDao<TchannelM,String> tchannelMDao;
	
	public String version = "1.0";

	
	/**
	 * 获得有头的 Map参数
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> getMapParam(String mcode,String tcode)throws Exception{

		Map<String,Object> result = new HashMap<String, Object>();
		result.put("mcode", mcode);
		result.put("tcode", tcode);
		result.put("version", version);
		return result;
	}

	public boolean isFormal(){
		return ConConfig.isFormal() && "1".equals(debug);
	}
	
	/**
	 * 查询余额
	 * @param send
	 * @return
	 * @throws Exception
	 */
	public BalanceResult balance(Qian4SendLog send)throws Exception{
        Map map = getMiYao(send.getRoleId());
        String mcode = map.get("mcode").toString();
        String tcode = map.get("tcode").toString();
        String mainKey = map.get("mainKey").toString();
        String pinKey = map.get("pinKey").toString();
        String trkKey = map.get("trkKey").toString();

		Map<String,Object> param = getMapParam(mcode,tcode);
		String result = "";
		param.put("action", "balance_balanceEnquiry");
		param.put("functionKey", "BALANCEINQUIRY");
		param.put("merchantOrderId", send.getMerchantOrderId().substring(0,17));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String merchantOrderTimeStr = sdf.format(send.getMerchantOrderTime());
		param.put("merchantOrderTime", merchantOrderTimeStr.substring(0,14).toString());
		param.put("orgMerchantOrderId", send.getOrgMerchantOrderId());
		param.put("accountNumber", send.getAccountNumber());//卡号
		param.put("mpin", encryptPin(send.getPin(),send.getAccountNumber(),mainKey,pinKey));//密码
		param.put("mtrack2Data", encryptTrack(send.getTrack2Data(),mainKey,trkKey));//
		param.put("track3Data", null == send.getTrack3Data() ? "" : send.getTrack3Data());
		param.put("encryType", 0);
		if(StringUtils.isNotEmpty(send.getIcCardYu()))
			param.put("icCardYu", send.getIcCardYu());

		String paramStr = JSONStringBuilder.getAjaxString(param);

		wf.log("【中亿腾乐-查询余额】请求参数:"+paramStr, "调试");
		if(isFormal()){
			wf.log("【中亿腾乐-查询余额】请求环境:生产环境", "调试");
			result = ZytlHttpUrlToPost.httpClientUtils(paramStr, ZytlHttpUrlToPost.urlStr,mcode);
		}else{
			wf.log("【中亿腾乐-查询余额】请求环境:测试环境", "调试");
			result = "{\"bankResp\":\"1\",\"accountNumber\":\"625996000513545411\",\"reqMsg\":\"查询余额成功\",\"accountBalance\":\"808188\",\"reqCode\":\"0000\",\"bankName\":\"农业银行\",\"cardType\":\"信用卡\"}";
		}
		wf.log("【中亿腾乐-查询余额】响应数据:"+result, "调试");
		
		BalanceResult br = new BalanceResult();
		JSONStringBuilder js = new JSONStringBuilder(result);
		br.setReqCode(js.getJsonString("reqCode"));
		br.setReqMsg(js.getJsonString("reqMsg"));
		br.setStatus(0);
		if("0000".equals(br.getReqCode())){
			br.setStatus(1);
		}
		br.setAccountNumber(js.getJsonString("accountNumber"));
		br.setBalance(js.getJsonString("accountBalance"));
		br.setBankName(js.getJsonString("bankName"));
		br.setCardType(js.getJsonString("cardType"));
		br.setBalanceCurrency(js.getJsonString("balanceCurrency"));
		return br;
	}
	
	/**
	 * 上传凭证
	 * @param transId
	 * @param signatureRelPath
	 * @return
	 * @throws Exception
	 */
	public String uploadImages(String mcode, String tcode, String transId,String signatureRelPath)throws Exception{
        String result = null;
		try{
			if(StringUtils.isEmpty(transId) || StringUtils.isEmpty(signatureRelPath)) return null;
			File file = new File(signatureRelPath);
			if(!file.exists()) return null;
			String picBuffer = new BASE64Encoder().encode(FileUtils.getFileBytes(file));
			Map<String,Object> param = getMapParam(mcode,tcode);
			param.put("action", "purchase_picUpload");
			param.put("transId", transId);
			param.put("picBuffer", picBuffer);
			String paramStr = JSONStringBuilder.getAjaxString(param);
			result = ZytlHttpUrlToPost.httpClientUtils(paramStr, ZytlHttpUrlToPost.urlStr,mcode);
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 消费
	 * @param send
	 * @return
	 * @throws Exception
	 */
	public PurchaseResult purchase(Qian4SendLog send,String signatureRelPath) throws Exception {

        Map map = getMiYao(send.getRoleId());
        String mcode = map.get("mcode").toString();
        String tcode = map.get("tcode").toString();
        String mainKey = map.get("mainKey").toString();
        String pinKey = map.get("pinKey").toString();
        String trkKey = map.get("trkKey").toString();

        Map<String,Object> param = getMapParam(mcode,tcode);
		String result = "";
		param.put("action", "purchase_receivablesToCard");
		param.put("merchantOrderId", send.getMerchantOrderId());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String merchantOrderTimeStr = sdf.format(send.getMerchantOrderTime());
		param.put("merchantOrderTime", merchantOrderTimeStr.substring(0,14).toString());
		param.put("transAmt", send.getTransAmt());//金额分
		param.put("accountNumber", send.getAccountNumber());//卡号
        param.put("mpin", encryptPin(send.getPin(), send.getAccountNumber(), mainKey, pinKey));
        param.put("mtrack2Data", encryptTrack(send.getTrack2Data(), mainKey, trkKey));
		param.put("track3Data", null == send.getTrack3Data() ? "" : send.getTrack3Data());
		if(StringUtils.isNotEmpty(send.getIcCardYu()))
			param.put("icCardYu", send.getIcCardYu());
		param.put("transTerminalId", null == send.getTransTerminalId() ? "" : send.getTransTerminalId());
		//param.put("signatureRelPath", signatureRelPath);
		String paramStr = JSONStringBuilder.getAjaxString(param);
		wf.log("【中亿腾乐-消费（收款）】请求参数:"+paramStr, "调试");
		if(isFormal()){
			wf.log("【中亿腾乐-消费（收款）】请求环境:生产环境", "调试");
			result = ZytlHttpUrlToPost.httpClientUtils(paramStr, ZytlHttpUrlToPost.urlStr,mcode);
		}else{
			wf.log("【中亿腾乐-消费（收款）】请求环境:测试环境", "调试");
			result = "{\"batchId\":\"\",\"searchId\":\"\",\"acquirersName\":\"\",\"authorizationCode\":\"\",\"terminalId\":\"7030008\",\"transMoney\":0.01,\"transTime\":\"09:44:16\",\"merchantName\":\"北京中亿腾乐有限公司\",\"merchantId\":\"173665683923452\",\"reqMsg\":\"成功\",\"status\":3,\"reqCode\":\"0000\",\"orderNum\":\"2014102309441600000221854983\",\"bankRef\":\"145353\",\"cardType\":\"\",\"bankCard\":\"6226220112722482\",\"bankResp\":\"1\",\"bankTransTime\":\"2014/10/23\",\"category\":\"收款\",\"issueBank\":\"民生银行\",\"transId\":\"\",\"expireDate\":\"\",\"receivablesId\":461510,\"merchantOrderId\":\"20141023094416347886888\"}";
		}
		wf.log("【中亿腾乐-消费（收款）】响应数据:"+result, "调试");
		JSONStringBuilder js = new JSONStringBuilder(result);
		PurchaseResult pr = new PurchaseResult();
		pr.setReqCode(js.getJsonString("reqCode"));
		pr.setReqMsg(js.getJsonString("reqMsg"));
		pr.setStatus(0);
		if("0000".equals(pr.getReqCode())){
			pr.setStatus(1);
		}
		pr.setBankTransTime(js.getJsonString("transTime"));
		pr.setBankOrderNum(js.getJsonString("transId"));
//		pr.setAcquirers(js.getJsonString("acquirersName"));
		pr.setAcquirersName(js.getJsonString("acquirersName"));
		pr.setIssueBank(js.getJsonString("issueBank"));
		pr.setCardType(js.getJsonString("cardType"));
		pr.setExpireDate(js.getJsonString("expireDate"));
		pr.setBatchId(js.getJsonString("batchId"));
		pr.setSearchId(js.getJsonString("searchId"));
		pr.setAuthorizationCode(js.getJsonString("authorizationCode"));
		pr.setBankRef(js.getJsonString("bankRef"));
		pr.setAccountNumber(js.getJsonString("accountNumber"));
		//pr.setBankTransDate(js.getJsonString("bankTransDate"));
		
		uploadImages(mcode,tcode,js.getJsonString("transId"),signatureRelPath);
		return pr;
	}
	
	/**
	 * 撤销冲正
	 * @param send
	 * @return
	 * @throws Exception
	 */
	public RevocationResult revocation(Qian4SendLog send)throws Exception{

        String mcode = "";
        String tcode = "";

        if(StringUtils.isBlank(send.getMerchantId()) || StringUtils.isBlank(send.getTerminalId())){
            Map map = getMiYao(send.getRoleId());
            mcode = map.get("mcode").toString();
            tcode = map.get("tcode").toString();
        }else {
            mcode = send.getMerchantId();
            tcode = send.getTerminalId();
        }

		Map<String,Object> param = getMapParam(mcode,tcode);
		param.put("action", "revocationNL_revocation");
		param.put("functionKey", "RECEIVABLES_REVOCATION");
		param.put("merchantOrderId", send.getMerchantOrderId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String merchantOrderTimeStr = sdf.format(send.getMerchantOrderTime());
        param.put("merchantOrderTime", merchantOrderTimeStr.substring(0,14).toString());
		param.put("orgMerchantOrderId", send.getOrgMerchantOrderId());
		param.put("transAmt", send.getTransAmt());
		String paramStr = JSONStringBuilder.getAjaxString(param);
		wf.log("【中亿腾乐-撤销冲正】请求参数:" + paramStr, "调试");
		String result = "";
		if(isFormal()){
			wf.log("【中亿腾乐-撤销冲正】请求环境:生产环境", "调试");
			result = ZytlHttpUrlToPost.httpClientUtils(paramStr, ZytlHttpUrlToPost.urlStr,mcode);
		}else{
			wf.log("【中亿腾乐-撤销冲正】请求环境:测试环境", "调试");
			result = "{\"reqMsg\":\"撤销成功\",\"reqCode\":\"0000\"}";
		}
		wf.log("【中亿腾乐-撤销冲正】响应数据:" + result, "调试");
		JSONStringBuilder js = new JSONStringBuilder(result);
		RevocationResult rr = new RevocationResult();
		rr.setReqCode(js.getJsonString("reqCode"));
		rr.setStatus(0);
		if("0000".equals(rr.getReqCode())){
			rr.setStatus(1);
		}
		rr.setReqMsg(js.getJsonString("reqMsg"));
		rr.setBankTransTime(js.getJsonString("bankTransTime"));
		rr.setExpireDate(js.getJsonString("expireDate"));
		rr.setBankOrderNum(js.getJsonString("bankOrderNum"));
		rr.setBatchId(js.getJsonString("batchId"));
		rr.setSearchId(js.getJsonString("searchId"));
		rr.setAuthorizationCode(js.getJsonString("authorizationCode"));
		rr.setBankRef(js.getJsonString("bankRef"));
		rr.setAccountNumber(js.getJsonString("accountNumber"));
		rr.setBankTransDate(js.getJsonString("bankTransDate"));
		return rr;
	}
	
	/**
	 * 信用卡还款
	 * @param send
	 * @return
	 * @throws Exception
	 */
	public PurchaseResult creditRepayment(Qian4SendLog send)throws Exception{

        Map map = getMiYao(send.getRoleId());
        String mcode = map.get("mcode").toString();
        String tcode = map.get("tcode").toString();
        String mainKey = map.get("mainKey").toString();
        String pinKey = map.get("pinKey").toString();
        String trkKey = map.get("trkKey").toString();

        Map<String,Object> param = getMapParam(mcode,tcode);
		String result = "";
		param.put("action", "creditR_creditRepayment");
		param.put("merchantOrderId", send.getMerchantOrderId());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = sdf.format(send.getMerchantOrderTime()).substring(0,14).toString();
		param.put("merchantOrderTime", dateStr);
		param.put("transAmt", send.getTransAmt());
		param.put("accountNumber", send.getAccountNumber());//卡号
        param.put("mpin", encryptPin(send.getPin(), send.getAccountNumber(), mainKey, pinKey));
        param.put("mtrack2Data", encryptTrack(send.getTrack2Data(), mainKey, trkKey));
		param.put("track3Data", null == send.getTrack3Data() ? "" : send.getTrack3Data());
		param.put("creditNumber", send.getCreditNumber());
		if(StringUtils.isNotEmpty(send.getIcCardYu()))
			param.put("icCardYu", send.getIcCardYu());
		param.put("isNeedNameId", 	send.getIsNeedNameId());
		if(null != send.getIsNeedNameId() && 1 == send.getIsNeedNameId().intValue()){
			param.put("idCard", 	send.getIsNeedNameId());
			param.put("purchaseName", 	send.getPurchaseName());
		}
		String paramStr = JSONStringBuilder.getAjaxString(param);
		wf.log("【中亿腾乐-信用卡还款】请求参数:"+paramStr, "调试");
		if(isFormal()){
			wf.log("【中亿腾乐-信用卡还款】请求环境:生产环境", "调试");
			result = ZytlHttpUrlToPost.httpClientUtils(paramStr, ZytlHttpUrlToPost.urlStr,mcode);
		}else{
			wf.log("【中亿腾乐-信用卡还款】请求环境:测试环境", "调试");
			result = "{\"reqMsg\":\"还款成功\",\"reqCode\":\"0000\"}";
		}
		wf.log("【中亿腾乐-信用卡还款】响应数据:"+result, "调试");
		
		JSONStringBuilder js = new JSONStringBuilder(result);
		PurchaseResult pr = new PurchaseResult();
		pr.setReqCode(js.getJsonString("reqCode"));//应答码
		pr.setReqMsg(js.getJsonString("reqMsg"));//应答码描述
		pr.setStatus(0);
		if("0000".equals(pr.getReqCode())){
			pr.setStatus(1);
		}
		pr.setBankOrderNum(js.getJsonString("transId"));//交易流水号
		pr.setBankTransTime(js.getJsonString("transTime"));//交易时间
		pr.setAccountNumber(send.getAccountNumber());//
		pr.setCreditNumber(send.getCreditNumber());
		pr.setAcquirersName(js.getJsonString("acquirersName"));//收单行
		pr.setIssueBank(js.getJsonString("issueBank"));//发卡行名称
		pr.setCardType(js.getJsonString("cardType"));//卡类别
		pr.setExpireDate(js.getJsonString("expireDate"));//有效期
		pr.setBatchId(js.getJsonString("batchId"));//批次号
		pr.setSearchId(js.getJsonString("searchId"));//查询号
		pr.setAuthorizationCode(js.getJsonString("authNum"));//授权号
		pr.setBankRef(js.getJsonString("refId"));//参考和
		return pr;
	}
	
	/**
	 * 检测信用卡是否可以还款
	 * @param transAmt 单位分
	 * @return
	 * @throws Exception
	 */
	public FeeResult checkCreditBankCard(String creditNumber,String transAmt,long roleId)throws Exception{

        Map map = getMiYao(roleId);
        String mcode = map.get("mcode").toString();
        String tcode = map.get("tcode").toString();

        Map<String,Object> param = getMapParam(mcode,tcode);
		String result = "";
		param.put("action", "creditR_checkCreditBankCard");
		param.put("creditNumber", creditNumber);
		param.put("transAmt", transAmt);
		String paramStr = JSONStringBuilder.getAjaxString(param);
		System.out.println("//00//:"+paramStr);
		wf.log("【中亿腾乐-检测信用卡是否可以还款】请求参数:"+paramStr, "调试");
		if(isFormal()){
			wf.log("【中亿腾乐-检测信用卡是否可以还款】请求环境:生产环境", "调试");
			result = ZytlHttpUrlToPost.httpClientUtils(paramStr, ZytlHttpUrlToPost.urlStr,mcode);
		}else{
			wf.log("【中亿腾乐-检测信用卡是否可以还款】请求环境:测试环境", "调试");
			result = "{\"reqMsg\":\"该卡可以进行还款\",\"reqCode\":\"0000\",\"fee\":300,\"isNeedNameId\":1}";
		}
		wf.log("【中亿腾乐-检测信用卡是否可以还款】响应数据:"+result, "调试");
		
		JSONStringBuilder js = new JSONStringBuilder(result);
		FeeResult fr = new FeeResult();
		fr.setStatus(0);
		if("0000".equals(js.getJsonString("reqCode")))
			fr.setStatus(1);
		fr.setAccountNumber(creditNumber);
		fr.setReqCode(js.getJsonString("reqCode"));//应答码
		fr.setReqMsg(js.getJsonString("reqMsg"));//应答码描述
		fr.setFee(js.getJsonString("fee"));//手续费
		fr.setIsNeedNameID(js.getJsonString("isNeedNameId"));//是否需要传入身份证和姓名
		return fr;
	}
	
	/**
	 * 卡卡转账
	 * @param send
	 * @return
	 * @throws Exception
	 */
	public PurchaseResult transfer(Qian4SendLog send)throws Exception{

        Map map = getMiYao(send.getRoleId());
        String mcode = map.get("mcode").toString();
        String tcode = map.get("tcode").toString();
        String mainKey = map.get("mainKey").toString();
        String pinKey = map.get("pinKey").toString();
        String trkKey = map.get("trkKey").toString();

        Map<String,Object> param = getMapParam(mcode,tcode);
		String result = "";
		param.put("action", "transfer_transfer");
		param.put("merchantOrderId", send.getMerchantOrderId().substring(0,17));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = sdf.format(send.getMerchantOrderTime()).substring(0,14).toString();
        param.put("merchantOrderTime", dateStr);
		param.put("transAmt", send.getTransAmt());//金额分
		param.put("accountNumber", send.getAccountNumber());//卡号
        param.put("mpin", encryptPin(send.getPin(), send.getAccountNumber(), mainKey, pinKey));
        param.put("mtrack2Data", encryptTrack(send.getTrack2Data(), mainKey, trkKey));
		param.put("track3Data", null == send.getTrack3Data() ? "" : send.getTrack3Data());
		param.put("accountNumber2", send.getAccountNumber2());//转入卡卡号
		param.put("accountName", send.getAccountName());//转入账户名
		
		if(StringUtils.isNotEmpty(send.getIcCardYu()))
			param.put("icCardYu", send.getIcCardYu());
		param.put("isNeedNameId", 	send.getIsNeedNameId());
		if(null != send.getIsNeedNameId() && 1 == send.getIsNeedNameId().intValue()){
			param.put("idCard", send.getIdCard());
			param.put("purchaseName", send.getPurchaseName());
		}
		String paramStr = JSONStringBuilder.getAjaxString(param);
		wf.log("【中亿腾乐-卡卡转账】请求参数:"+paramStr, "调试");
		if(isFormal()){
			wf.log("【中亿腾乐-卡卡转账】请求环境:生产环境", "调试");
			result = ZytlHttpUrlToPost.httpClientUtils(paramStr, ZytlHttpUrlToPost.urlStr,mcode);
		}else{
			wf.log("【中亿腾乐-卡卡转账】请求环境:测试环境", "调试");
			result = "{\"reqMsg\":\"转账成功\",\"reqCode\":\"0000\"}";
		}
		wf.log("【中亿腾乐-卡卡转账】响应数据:"+result, "调试");
		
		JSONStringBuilder js = new JSONStringBuilder(result);
		PurchaseResult pr = new PurchaseResult();
		pr.setReqCode(js.getJsonString("reqCode"));//应答码
		pr.setReqMsg(js.getJsonString("reqMsg"));//应答码描述
		pr.setStatus(0);
		if("0000".equals(pr.getReqCode())){
			pr.setStatus(1);
		}
		pr.setBankOrderNum(js.getJsonString("transId"));//交易流水号
		pr.setBankTransTime(js.getJsonString("transTime"));//交易时间
		pr.setAccountNumber(send.getAccountNumber());//
		pr.setCreditNumber(send.getCreditNumber());
		pr.setAcquirersName(js.getJsonString("acquirersName"));//收单行
		pr.setIssueBank(js.getJsonString("issueBank"));//发卡行名称
		pr.setCardType(js.getJsonString("cardType"));//卡类别
		pr.setExpireDate(js.getJsonString("expireDate"));//有效期
		pr.setBatchId(js.getJsonString("batchId"));//批次号
		pr.setSearchId(js.getJsonString("searchId"));//查询号
		pr.setAuthorizationCode(js.getJsonString("authNum"));//授权号
		pr.setBankRef(js.getJsonString("refId"));//参考和
		
		return pr;
	}
	
	/**
	 * 检查卡卡转账
	 * @param accountNumber2
	 * @param transAmt
	 * @return
	 * @throws Exception
	 */
	public FeeResult checkTransfer(String accountNumber2,String transAmt,long roleId)throws Exception{
        Map map = getMiYao(roleId);
        String mcode = map.get("mcode").toString();
        String tcode = map.get("tcode").toString();

		Map<String,Object> param = getMapParam(mcode,tcode);
		String result = "";
		param.put("action", "transfer_checkTransfer");
		param.put("accountNumber2", accountNumber2);
		param.put("transAmt", transAmt);
		String paramStr = JSONStringBuilder.getAjaxString(param);
		wf.log("【中亿腾乐-检测入账卡是否可以转账】请求参数:" + paramStr, "调试");
		if(isFormal()){
			wf.log("【中亿腾乐-检测入账卡是否可以转账】请求环境:生产环境", "调试");
			result = ZytlHttpUrlToPost.httpClientUtils(paramStr, ZytlHttpUrlToPost.urlStr,mcode);
		}else{
			wf.log("【中亿腾乐-检测入账卡是否可以转账】请求环境:测试环境", "调试");
			result = "{\"reqMsg\":\"该卡可以进行还款\",\"reqCode\":\"0000\",\"fee\":300,\"isNeedNameId\":1}";
		}
		wf.log("【中亿腾乐-检测入账卡是否可以转账】响应数据:" + result, "调试");
		
		JSONStringBuilder js = new JSONStringBuilder(result);
		FeeResult fr = new FeeResult();
		if("0000".equals(js.getJsonString("reqCode")))
			fr.setStatus(1);
		fr.setAccountNumber(accountNumber2);
		fr.setReqCode(js.getJsonString("reqCode"));//应答码
		fr.setReqMsg(js.getJsonString("reqMsg"));//应答码描述
		fr.setFee(js.getJsonString("fee"));//手续费
		fr.setIsNeedNameID(js.getJsonString("isNeedNameId"));//是否需要传入身份证和姓名
		return fr;
	}

    public Map getMiYao(long roleId)throws Exception{

		if(roleId==1){
			roleId=2;
		}
        Map<String,String> miYao = new HashMap<String, String>();
        TchannelM tcm = null;
        List<TchannelM> list = tchannelMDao.searchMobileTCListByCodeOrderByUseTime(PurchaseBaseAction.TC_ZHONGYI,roleId);
        tcm = list.get(0);//获取第一个
        tchannelMDao.updateTchannelMUseTime(tcm.getId());
        String otherParam = tcm.getOtherParam();
        System.out.println("otherParam: "+otherParam);
        String[] strA = otherParam.split("_");
        miYao.put("pinKey", strA[3].toString());
        miYao.put("trkKey", strA[5].toString());
        miYao.put("mainKey",tcm.getMainKey().toString());
        miYao.put("mcode",tcm.getMcode().toString());
        miYao.put("tcode",tcm.getTcode().toString());
        return miYao;
    }

    public Qian4SendLog initSend(Qian4SendLog send) throws Exception {
        Map map = getMiYao(send.getRoleId());// 获取密钥  pinKey trkKey mainKey  mcode tcode 的密钥
        String mcode = map.get("mcode").toString(); //获取 mcode
        String tcode = map.get("tcode").toString(); //获取 tcode
        send.setMerchantId(mcode);
        send.setTerminalId(tcode);
        return send;
    }

    /**
     * 加密track2
     * @param
     * @return
     */
    public String encryptTrack(String track,String mainKey,String trackKey) throws Exception {
        String trk = "";
        String trackKeyD = Des3.decode3DES(trackKey, mainKey);
        if(!StringUtils.isBlank(track)){
            trk = Des3.encryptStanderTranck(trackKeyD, track);
        }
        return trk;
    }

    /**
     * 加密pin
     * @param
     * @return
     */
    public String encryptPin(String pin,String accountNum,String mainKey,String pinKey) throws Exception {
        String pinM = "";
        //解密pin秘钥和track秘钥
        String pinKeyD = Des3.decode3DES(pinKey, mainKey);
        //拿解密出来的明文pin秘钥和track秘钥加密pin,track2
        if(!StringUtils.isBlank(pin)){
           pinM = Des3.encryptPin(accountNum, pin, pinKeyD);
        }
        return pinM;
    }

}
